<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;

class UserController extends Controller
{
    /*
		用户列表
    */
	public function getIndex(Request $request)
	{

		
		//获取数据
		// $users = DB::table('user')->get();

		//获取每页显示条数
		$num = $request->input('num',10);
		
		//获取参数列表
		$list = $request->all();

		//判断是否有查询条件
		if($request->input('keywords')){
			$users = DB::table('shop_users')->where('uname','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$users = DB::table('shop_users')->paginate($num);
		}

		//解析模板
		return view('admin.user.index',['users'=>$users,'list'=>$list,'num'=>$num]);
	}

	/*
		用户添加
	*/
	public function getAdd()
	{
		//后台用户添加模板
		return view('admin.user.add');
	}

	/*
		执行添加
	*/
	public function postInsert(UserInsertRequest $request)
	{
		//提取数据
		$data = $request->only(['uname','upwd','email','tel','auth','sex']);

		//验证表单用户名、手机号、邮箱是否唯一
		$users = DB::table('shop_users')->where('uname',$data['uname'])->first();
		if($users){
			return back()->with('error','用户已存在');
		}
		$tel = DB::table('shop_users')->where('tel',$data['tel'])->first();
		if($tel){
			return back()->with('error','手机已存在');
		}
		$email = DB::table('shop_users')->where('email',$data['email'])->first();
		 if($email){
			return back()->with('error','邮箱已存在');
		}
		
		//处理token
		$data['token'] = str_random(50);
		
		//密码加密
		$data['upwd'] = Hash::make($data['upwd']);
		
		//调用方法上传头像
		$data['uface'] = self::upload($request,'uface','users');

		$data['regtime'] = time();
		//执行数据插入
		$res = DB::table('shop_users')->insert($data);
		if($res){
			return redirect('/admin/user/index')->with('success','用户添加成功');
		}else{
			return back()->with('error','用户添加失败');
		}

	}

		/*
		头像上传方法
	*/	
	public static function upload($request,$iname,$tab)
	{
		if ($request->hasFile($iname)) {
    		//获取文件后缀名
    		$suffix = $request->file($iname)->getClientOriginalExtension();

    		//随机文件名
    		$name = md5(time().rand(1,99999));
    		$request->file($iname)->move("./uploads/{$tab}",$name.'.'.$suffix);
    		return '/uploads/'.$tab.'/'.$name.'.'.$suffix;
    		
		}
	}


	/*
		用户删除
	*/
	public function postDelete(Request $request)
	{
		$id = $request->input('id');

		//删除图片 
		$path = DB::table('shop_users')->where('uid',$id)->value('uface');

		//删除用户  数据库删除
		$res = DB::table('shop_users')->where('uid',$id)->delete();

		//删除本地
		if(!empty($path)){
			$path = '.'.$path;
			if(file_exists($path))
			{
				
				unlink($path);
			}
		}
		echo $res;
	}

	/*
		用户修改
	*/
	public function getEdit(Request $request)
	{
		$id = $request->input('id');

		$users = DB::table('shop_users')->where('uid',$id)->first();
		//解析模板

		return view('admin/user/edit',['users'=>$users]);
	}


	/*
		执行用户修改
	*/
	public function postUpdate(UserUpdateRequest $request)
	{
		//处理数据
		$data = $request->except('_token');
		//验证表单用户名、手机号、邮箱是否唯一
		$user = DB::table('shop_users')->where('uid',$data['uid'])->first();
		if($data['uname'] != $user->uname){
			$users = DB::table('shop_users')->where('uname',$data['uname'])->first();
			if($users){
				return back()->with('error','用户已存在');
			}
		}
		if($data['tel'] != $user->tel){
		$tel = DB::table('shop_users')->where('tel',$data['tel'])->first();
			if($tel){
				return back()->with('error','手机已存在');
			}
		}
		if($data['email'] != $user->email){
			$email = DB::table('shop_users')->where('email',$data['email'])->first();
			 if($email){
				return back()->with('error','邮箱已存在');
			}
		}
		$data['updtime'] = time();
		//判断图片是否上传
		if($request->hasFile('uface')){
			$data['uface'] = self::upload($request,'uface','users');

		}
		$res = DB::table('shop_users')->where('uid', $request->input('uid'))->update($data);
   		if($res){
   			//成功
   			return redirect('/admin/user/index')->with('success','修改成功');
   		}else{
   			//失败
   			return back()->with('error','用户修改失败');

		}
	}

	/*
		密码修改
	*/
	public function getEditpw(Request $request)
	{
		$id = $request->input('id');

		$users = DB::table('shop_users')->where('uid',$id)->first();
		//解析模板

		return view('admin/user/editpw',['users'=>$users]);
	}

	/*
		执行密码修改
	*/
	public function postDoedit(Request $request)
	{
		//处理数据
		$data = $request->except('_token');
		
		//密码非空以及格式判断
		if(empty($data['upwd'])){
			return back()->with('error','密码不能为空');
		}else{
			if(!preg_match('/\w{6,18}/',$data['upwd'])){
				return back()->with('error','密码格式不正确');
			}
		}
		//确认密码 
		if($data['reupwd'] != $data['upwd']){
			return back()->with('error','两次密码不一致');
		}

		//加密
		$data['upwd'] = Hash::make($data['upwd']);
		$arr = ['upwd'=>$data['upwd']];
		//修改密码
		$res = DB::table('shop_users')->where('uid',$data['uid'])->update($arr);
		if($res){
			return redirect('/admin/user/index')->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}
	}
}
